package com.bawei.week.repair

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.mvvm.BaseActivity
import com.bawei.week.R
import com.bawei.week.adapter.RepairAdapter
import com.bawei.week.databinding.ActivityRepairBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/RepairActivity")
@AndroidEntryPoint
class RepairActivity : BaseActivity<ActivityRepairBinding,RepairViewModel>() {
    val repairAdapter by lazy { RepairAdapter() }
    var flage = false
    override fun initView() {
        binding.rvRepair.apply {
            adapter = repairAdapter
            layoutManager = LinearLayoutManager(this@RepairActivity)
        }

        binding.ivAdd.setOnClickListener {
            ARouter.getInstance().build("/app/RepairAddActivity").navigation()
        }

        binding.repairOrder.setOnClickListener {
            if (flage){
                repairAdapter.data.sortByDescending {
                    it.repairTime
                }
            }else{
                repairAdapter.data.sortBy {
                    it.repairTime
                }
            }
            repairAdapter.notifyDataSetChanged()
            flage = !flage
        }

        binding.repairSearch.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                vm.getRepairSearch(query!!)
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return true
            }
        })
    }

    override fun initData() {
        vm.repairSuc.observe(this){
            repairAdapter.data.clear()
            repairAdapter.addData(it)
            repairAdapter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getRepair()
    }
}